﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using Lucene.Net.Store;
//using Lucene.Net.Analysis;
//using Lucene.Net.Index;
//using Lucene.Net.Documents;
//using Lucene.Net.QueryParsers;
//using Lucene.Net.Search;
//using Lucene.Net.Analysis.Standard;
//using System.Diagnostics;
//using System.IO;

//namespace Lucene101.Service
//{
//    public class Search
//    {
//        #region http://www.javaranch.com/journal/2004/04/Lucene.html

//        //private Lucene.Net.Documents.Document createDocument(String article, String author,
//        //                     String title, String topic,
//        //                     String url, DateTime dateWritten)
//        //{

//        //    var document = new Lucene.Net.Documents.Document();
//        //    document.Add(Lucene.Net.Documents.Field.Text("author", author));
//        //    document.Add(Lucene.Net.Documents.Field.Text("title", title));
//        //    document.Add(Lucene.Net.Documents.Field.Text("topic", topic));
//        //    document.Add(Lucene.Net.Documents.Field.UnIndexed("url", url));
//        //    document.Add(Lucene.Net.Documents.Field.Keyword("date", dateWritten));
//        //    document.Add(Lucene.Net.Documents.Field.UnStored("article", article));
//        //    return document;
//        //}

//        //String indexDirectory = "lucene-index";

//        //private void indexDocument(Lucene.Net.Documents.Document document)
//        //{
//        //    Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
//        //    Lucene.Net.Store.Directory directory = new Lucene.Net.Store.SimpleFSDirectory(new System.IO.FileInfo(indexDirectory));
//        //    Lucene.Net.Index.IndexWriter writer = new Lucene.Net.Index.IndexWriter(directory, analyzer, false);
//        //    writer.AddDocument(document);
//        //    writer.Optimize();
//        //    writer.Close();
//        //} 

//        #endregion

//        #region codeclimber.net.nz

//        // http://codeclimber.net.nz/archive/2009/09/02/lucene.net-your-first-application.aspx
//        // http://codeclimber.net.nz/archive/2009/09/04/dissecting-lucene.net-storage-documents-and-fields.aspx

//        private static Lucene.Net.Util.Version luceneVersion = Lucene.Net.Util.Version.LUCENE_29;
//        private static System.IO.DirectoryInfo indexDir = new System.IO.DirectoryInfo("LuceneIndex");

//        public static void BuildIndex()
//        {
//            var directory = FSDirectory.Open(indexDir);
//            Analyzer analyzer = new StandardAnalyzer(luceneVersion);

//            Debug.WriteLine("Reading birds from CSV file to memory...");
//            var birdSpecies = ReadBirdsFromCsvFile();
//            Debug.WriteLine(birdSpecies.Count().ToString() + " species read from file...");

//            Debug.WriteLine("Reading locations from CSV file to memory...");
//            var locations = ReadLocationsFromCsvFile();
//            Debug.WriteLine(locations.Count().ToString() + " locations read from file...");

//            Debug.WriteLine("Creating new index...");
//            IndexWriter writer = new IndexWriter(
//                directory,
//                analyzer,
//                true, // Create new index?
//                IndexWriter.MaxFieldLength.UNLIMITED
//                );

//            Debug.WriteLine("Adding species to index...");
//            foreach (var item in birdSpecies)
//            {
//                var doc = Data.BirdSpecies.CreateDocumentFrom(item);
//                writer.AddDocument(doc);
//            }

//            Debug.WriteLine("Adding locations to index...");
//            foreach (var item in locations)
//            {
//                var doc = Data.Location.CreateDocumentFrom(item);
//                writer.AddDocument(doc);
//            }

//            Debug.WriteLine("Optimising index...");
//            writer.Optimize();
//            //Close the writer
//            Debug.WriteLine("Commiting index...");
//            writer.Commit();
//            Debug.WriteLine("Closing index...");
//            writer.Close();
//            directory.Close();
//        }

//        public static IEnumerable<object> PerformSearch(string searchString)
//        {
//            var directory = FSDirectory.Open(indexDir);
//            Analyzer analyzer = new StandardAnalyzer(luceneVersion);

//            QueryParser parser = new QueryParser(
//                luceneVersion,
//                Data.BirdSpecies.Name,
//                analyzer
//                );
//            Query query = parser.Parse(searchString);

//            //Setup searcher
//            IndexSearcher searcher = new IndexSearcher(directory, true);
//            //Do the search
//            var hits = searcher.Search(query);

//            int results = hits.Length();
//            //Debug.WriteLine("Found {0} results", results);
//            var ret = new List<object>();
//            for (int i = 0; i < results; i++)
//            {
//                var result = hits.Doc(i);
//                var item = GetResultItemFromDocument(result);
//                ret.Add(item);
//                //float score = hits.Score(i);
//                //Debug.WriteLine("Result num {0}, score {1}", i + 1, score);
//                //Debug.WriteLine(species.ToString() + Environment.NewLine);
//            }

//            searcher.Close();
//            directory.Close();

//            return ret;
//        }

//        private static object GetResultItemFromDocument(Document document)
//        {
//            switch (document.Get(Data.DataType.TypeKey))
//            {
//                case Data.DataType.Location:
//                    return Data.Location.CreateFromDocument(document);
//                case Data.DataType.BirdSpecies:
//                    return Data.BirdSpecies.CreateFromDocument(document);
//                default:
//                    return document.ToString();
//            }

//        }

//        private static IEnumerable<ResultItems.BirdSpecies> ReadBirdsFromCsvFile()
//        {
//            var sr = new StreamReader(@"../../../SolutionItems/BirdSpecies.csv");
//            var reader = new CsvHelper.CsvReader(sr);
//            var birdSpecies = new List<ResultItems.BirdSpecies>();
//            while (reader.Read())
//            {
//                var bird = new ResultItems.BirdSpecies();
//                bird.Name = reader.GetField(0);
//                bird.ScientificName = reader.GetField(1);
//                bird.Rarity = reader.GetField(2);
//                birdSpecies.Add(bird);
//            }
//            sr.Close();

//            return birdSpecies;
//        }

//        private static IEnumerable<ResultItems.Location> ReadLocationsFromCsvFile()
//        {
//            var sr = new StreamReader(@"../../../SolutionItems/Locations.csv");
//            var reader = new CsvHelper.CsvReader(sr);
//            var locations = new List<ResultItems.Location>();
//            while (reader.Read())
//            {
//                var location = new ResultItems.Location();
//                location.Name = reader.GetField(0);
//                location.Latitude = float.Parse(reader.GetField(1));
//                location.Longitude = float.Parse(reader.GetField(2));
//                locations.Add(location);
//            }
//            sr.Close();

//            return locations;
//        }

//        #endregion
//    }
//}


//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using Lucene.Net.Documents;

//namespace Lucene101.Service.Data
//{
//    [System.Diagnostics.DebuggerDisplay("Name={Name}, Id={Id}, Phone={PhoneNumber}")]
//    class Location
//    {
//        public const string Name = "Name";
//        public const string Latitude = "Latitude";
//        public const string Longitude = "Longitude";

//        // http://www.getlatlon.com/

//        public static ResultItems.Location CreateFromDocument(Lucene.Net.Documents.Document document)
//        {
//            var location = new ResultItems.Location();

//            location.Name = document.Get(Name);
//            location.Latitude = float.Parse(document.Get(Latitude));
//            location.Longitude = float.Parse(document.Get(Longitude));

//            return location;
//        }

//        public static Lucene.Net.Documents.Document CreateDocumentFrom(ResultItems.Location location)
//        {
//            var doc = new Lucene.Net.Documents.Document();

//            doc.Add(new Field(DataType.TypeKey, DataType.Location, Field.Store.YES, Field.Index.NO));
//            doc.Add(new Field(Name, location.Name, Field.Store.YES, Field.Index.ANALYZED));
//            doc.Add(new Field(Latitude, location.Latitude.ToString(), Field.Store.YES, Field.Index.ANALYZED));
//            doc.Add(new Field(Longitude, location.Longitude.ToString(), Field.Store.YES, Field.Index.ANALYZED));

//            return doc;
//        }
//    }
//}



//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;

//namespace Lucene101.Service.Data
//{
//    class DataType
//    {
//        public const string TypeKey = "_type";

//        public const string BirdSpecies = "BirdSpecies";
//        public const string Location = "Location";
//    }
//}


